package net.poemcode.gzlib;

import java.util.HashMap;
import java.util.Map;

import android.content.Context;
import android.database.Cursor;

import static net.poemcode.gzlib.LibraryOpenHelper.*;

public class Cache {
	
	private static Cache cache;
	
	private static boolean isValid = false;
	
	private String notfound;
	
	private Map<String, String> libMap = new HashMap<String, String>();
	private Map<String, String> localMap = new HashMap<String, String>();
	private Map<String, String> cirMap = new HashMap<String, String>();
	private Map<String, String> stateMap = new HashMap<String, String>();
	
	private Cache(Context context) {
		notfound = context.getResources().getString(R.string.not_found);
		isValid = updateLib(context) > 0;
		isValid = isValid && (updateLocal(context) > 0);
		isValid = isValid && (updateCir(context) > 0);
		isValid = isValid && (updateState(context) > 0);
	}
	
	public int updateLib(Context context) {
		libMap.clear();
		Cursor libCur = context.getContentResolver().query(CONTENT_URI_LIB, null, null, null, null);
		if (libCur != null) {
			while (libCur.moveToNext()) {
				libMap.put(libCur.getString(0), libCur.getString(1));
			}
			libCur.close();
		}
		return libMap.size();
	}
	
	public int updateLocal(Context context) {
		localMap.clear();
		Cursor localCur = context.getContentResolver().query(CONTENT_URI_LOCAL, null, null, null, null);
		if (localCur != null) {
			while (localCur.moveToNext()) {
				localMap.put(localCur.getString(0), localCur.getString(1));
			}
			localCur.close();
		}
		return localMap.size();
	}
	
	public int updateCir(Context context) {
		cirMap.clear();
		Cursor cirCur = context.getContentResolver().query(CONTENT_URI_CIR, null, null, null, null);
		if (cirCur != null) {
			while (cirCur.moveToNext()) {
				cirMap.put(cirCur.getString(0), cirCur.getString(1));
			}
			cirCur.close();
		}
		return cirMap.size();
	}
	
	public int updateState(Context context) {
		stateMap.clear();
		Cursor stateCur = context.getContentResolver().query(CONTENT_URI_STATE, null, null, null, null);
		if (stateCur != null) {
			while (stateCur.moveToNext()) {
				stateMap.put(stateCur.getString(0), stateCur.getString(1));
			}
			stateCur.close();
		}
		return stateMap.size();
	}
 
	public static Cache getInstance(Context context) {
		if (cache == null) {
			cache = new Cache(context);
		}
		
		return cache;
	}

	public String getLibName(final String libCode) {
		String name = libMap.get(libCode);
		isValid = (name != null);
		name = isValid ? name : notfound;
		return name;
	}
	
	public String getLocalName(final String localCode) {
		String name = localMap.get(localCode);
		isValid = (name != null);
		name = isValid ? name : notfound;
		return name;
	}
	
	public String getCirName(final String cirCode) {
		String name = cirMap.get(cirCode);
		isValid = (name != null);
		name = isValid ? name : notfound;
		return name;
	}
	
	public String getStateName(final String stateCode) {
		String name = stateMap.get(stateCode);
		isValid = (name != null);
		name = isValid ? name : notfound;
		return name;
	}
	
	public boolean isValid() {
		return isValid;
	}
}
